<?php

namespace App\Http\Services;

use App\Http\Model\FinanceUserWallet;
use App\Http\Model\SystemConfig;
use App\Http\Model\User;
use App\Http\Model\ConsumeIpBlacklist;
use Illuminate\Support\Facades\Log;

class UserFilterServices extends CommonServices
{
    /**
     * @var User
     */
    protected $modelUser;

    /**
     * @var SystemConfig
     */
    protected $modelSystemConfig;

    /**
     * @var ConsumeIpBlacklist
     */
    protected $modelConsumeIpBlacklist;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->modelUser = new User();
        $this->modelSystemConfig = new SystemConfig();
        $this->modelConsumeIpBlacklist = new ConsumeIpBlacklist();
    }

    /**
     * Created at 2021/12/31 12:52
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $userId
     * @return bool
     */
    public function getUserFilterResult(int $userId): bool
    {
        $userInfo = $this->modelUser->get_user($userId, false);
        if (!$userInfo) {
            return false;
        }

        $userUuid = $userInfo['user_uid'];
        if ($this->checkUserWhiteList($userUuid)) {// 用户在白名单
            return true;
        }

        if ($this->checkUserBlackList($userUuid)) {// 用户在黑名单
            return false;
        }

        $userCity = getCityByIp();// 获取用户城市
        return !($this->checkAddressBlackList($userCity));
    }

    /**
     * 返回娱乐房过滤黑名单
     *
     * Created at 2021/12/31 12:54
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $userId
     * @return array
     */
    public function getRoomFilterResult($userId): array
    {
        $userInfo = $this->modelUser->get_user($userId, false);
        if (!$userInfo) {
            Log::info('getRoomFilterResult UserInfo Empty:', ['user_id'=> $userId]);
            return [];
        }

        $userUuid = $userInfo['user_uid'];
        if ($this->checkUserWhiteList($userUuid)) {// 用户在白名单
            Log::info('getRoomFilterResult User In WhiteList:', ['user_id'=> $userId]);
            return [];
        }

        $roomIdBlackList = $this->modelSystemConfig->getRoomFilterBlackList();
        if ($this->checkUserBlackList($userUuid)) {// 用户在黑名单
            Log::info('getRoomFilterResult User In WhiteList:', ['user_id'=> $userId, 'black_list'=> $roomIdBlackList]);
            return $roomIdBlackList;
        }

        $userCity = getCityByIp();// 获取用户城市
        // Log::info('getRoomFilterResult User City:', ['user_id'=> $userId, 'city'=> $userCity]);
        $result = $this->checkAddressBlackList($userCity) ? $roomIdBlackList: [];
        return $result;
    }

    /**
     * 检测用户是否在白名单
     *
     * Created at 2021/12/31 12:45
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param string $userUuid
     * @return bool
     */
    private function checkUserWhiteList(string $userUuid): bool
    {
        $whiteList = $this->modelSystemConfig->getUserFilterWhiteList();
        if (!$whiteList) {
            return false;
        }

        return in_array($userUuid, $whiteList);
    }

    /**
     * 检测用户是否在黑名单
     *
     * Created at 2021/12/31 12:45
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param string $userUuid
     * @return bool
     */
    private function checkUserBlackList(string $userUuid): bool
    {
        $blackList = $this->modelSystemConfig->getUserFilterBlackList();
        if (!$blackList) {
            return false;
        }

        return in_array($userUuid, $blackList);
    }

    /**
     * 检测用户是否在城市黑名单
     *
     * Created at 2021/12/31 12:45
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param string $userCity
     * @return bool
     */
    public function checkAddressBlackList(string $userCity): bool
    {
        if (!$userCity) {
            return false;
        }

        $blackList = $this->modelSystemConfig->getAddressFilterBlackList();
        if (!$blackList) {
            return false;
        }

        return in_array($userCity, $blackList);
    }

    public function inExchangeAndWithdrawUserBlacklist(int $userId):bool{
        $blacklist = $this->modelSystemConfig->getExchangeAndWithdrawUserBlacklist();
        return in_array($userId, $blacklist);
    }

    public function inConsumeUserBlacklist(int $userId):bool {
        // 白名单不检测非法用户 add by wj
        $whitelistUser = $this->modelSystemConfig->getConsumeUserWhitelist(); //[1403574];
        if (in_array($userId, $whitelistUser)) {
            return false;
        }

        $blacklist = $this->modelSystemConfig->getConsumeUserBlacklist();
        if (in_array($userId, $blacklist)) {
            return true;
        }

        //临沧的禁止消费
        // $blacklistCity = ['临沧'];

        // if (in_array(getCityByIp(), $blacklistCity)) {
        //     return true;
        // }

        $client_ip = client_ip();
        return $this->modelConsumeIpBlacklist->inIpBlacklist($client_ip);
    }

    public function hideFullScreen($userId): bool{
        list($blackCityList, $blackUserList, $whiteUserList,$version, $timeStamp) = $this->modelSystemConfig->getFullScreenHideConfig();
        if (in_array($userId, $blackUserList)){
            return true;
        }

        if (in_array($userId, $whiteUserList)){
            return false;
        }

        $userCity = getCityByIp();// 获取用户城市
        $userInfo = $this->modelUser->get_user($userId, true);
        $parse_app_UA = $this->parse_app_UA();
        write_log("UserFilter", 'hideFullScreen' . $userId, [$blackCityList, $blackUserList, $whiteUserList,$version, $timeStamp,$parse_app_UA['version_no'],$userInfo['create_timestamp'],$userCity]);
        if ((in_array($userCity, $blackCityList)
            && $userInfo['create_timestamp'] >  $timeStamp)
            ) {
            return true;
        }

        return false;
    }

    public function hideFullScreenAndAdEntrance($userId, $adName = ''): bool{
        return true;
    }

    public static function inForbidRegisterPhoneList($phone){
        list($phoneList, $idCardList) = (new SystemConfig())->getForbidRegisterAndVerifyList();
        $phoneList = explode(',', $phoneList);
        return in_array(trim($phone),$phoneList);
    }

    public static function inForbidVerifyIdCardList($idcard){
        list($phoneList, $idCardList) = (new SystemConfig())->getForbidRegisterAndVerifyList();
        $idCardList = explode(',', $idCardList);
        return in_array(trim($idcard),$idCardList);
    }

}